package cn.tb.org.service.impl;

import cn.tb.basic.exception.BusinessException;
import cn.tb.basic.query.BaseQuery;
import cn.tb.basic.service.impl.BaseServiceImpl;
import cn.tb.basic.util.BaiduAuditUtils;
import cn.tb.basic.util.PageList;
import cn.tb.org.domain.Employee;
import cn.tb.org.domain.Shop;
import cn.tb.org.domain.ShopAuditLog;
import cn.tb.org.mapper.EmployeeMapper;
import cn.tb.org.mapper.ShopAuditLogMapper;
import cn.tb.org.mapper.ShopMapper;
import cn.tb.org.query.ShopQuery;
import cn.tb.org.service.IEmployeeService;
import cn.tb.org.service.IShopAuditLogService;
import cn.tb.org.service.IShopService;
import cn.tb.org.vo.ShopVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tangbo
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 审核通过
     * 1.修改状态
     * 2.发送邮件
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(1);
        shopMapper.update(shop);
        //保存日志
        shopAuditLog.setAudit_id(333L);
        shopAuditLog.setState(1);
        shopAuditLogMapper.save(shopAuditLog);
        //发送邮件,发送给店长
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShop_id());
        SimpleMailMessage message = new SimpleMailMessage();
        //设置发送人
        message.setFrom("2798580754@qq.com");
        //邮件主题
        message.setSubject("审核");
        //邮件内容
        message.setText("亲，您的审核已通过，请点击激活连接后使用"+"激活链接：http://localhost:8080/shop/activation/"+shopAuditLog.getShop_id());
        //收件人
        //message.setTo(employee.getEmail());
        message.setTo("2798580754@qq.com");

        javaMailSender.send(message);

    }

    /**
     * 审核驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(2);
        shopMapper.update(shop);
        //保存日志
        shopAuditLog.setAudit_id(333L);
        shopAuditLog.setState(2);
        shopAuditLogMapper.save(shopAuditLog);
        //发送邮件
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShop_id());
        SimpleMailMessage message = new SimpleMailMessage();
        //设置发送人
        message.setFrom("2798580754@qq.com");
        //邮件主题
        message.setSubject("审核");
        //邮件内容
        message.setText("亲，您的审核已驳回，请点击修改链接："+"：http://localhost:8081/#/register2?shopId="+shopAuditLog.getId());
        //收件人
        //message.setTo(employee.getEmail());
        message.setTo("2798580754@qq.com");

        javaMailSender.send(message);
    }

    /**
     * 批量新增
     * @param shops
     */
    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    /**
     *报表  不同状态的部门数量
     * @return
     */
    @Override
    public List<ShopVo> findEcharts() {
        return shopMapper.queryEcharts();
    }

    /**
     * 激活
     */
    @Override
    public void activation(Long shop_id) {
        Shop shop = shopMapper.loadById(shop_id);
        shop.setState(3);
        shopMapper.update(shop);

    }

//    @Override
//    public PageList<Shop> queryPage(ShopQuery query) {
//        //查询总条数
//        Long total = shopMapper.queryCount(query);
//        //查询当前页数据
//        List<Shop> rows =shopMapper.queryData(query);
//
//        for (Shop row : rows) {
//            List<ShopAuditLog> byShop_id = shopAuditLogMapper.findByShop_id(row.getId());
//            row.setShopAuditLogs(byShop_id);
//        }
//        return new PageList<Shop>(total,rows);
//    }


    /**
     * 店铺入驻业务实现
     * 1.参数空校验
     * 2.合法校验
     * 3.判断是否已经注册
     *          1.店铺已经注册
     *          2.人已经注册
     *                 2.1.人已经注册还没有绑定店铺
     *                 2.2.人已经注册也绑定了店铺
     * 4.保存店铺
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
      //参数空校验
//      if(StringUtils.isEmpty(shop.getName())
//              ||StringUtils.isEmpty(shop.getAddress())
//              ||StringUtils.isEmpty(shop.getAdmin().getUsername())){
//          throw new BusinessException("参数不能为空！！！");
//      }
//        //文本审核
//        if(!BaiduAuditUtils.TextCensor(shop.getName())){
//            throw new BusinessException("不准乱写，小心打你!");
//        }
//        //判断是否注册
//        //判断店铺是否注册
//        Shop shopTmp = shopMapper.loadByName(shop.getName());
//        if(shopTmp!=null){
//            throw new BusinessException("店铺已经存在！");
//        }
//
//        Employee empTmp = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
//        //已经注册并且绑定了
//        if(empTmp!=null && empTmp.getShop_id()!=null){
//            throw new BusinessException("该用户已经有绑定店铺，忘记密码请找回");
//        }
//        //没绑定
//        if(empTmp==null){
//            employeeMapper.save(shop.getAdmin());
//            empTmp = shop.getAdmin();
//        }
//        //保存数据
//        shop.setAdminId(empTmp.getId());
//        shopMapper.save(shop);
//        empTmp.setShop_id(shop.getId());
//        employeeMapper.update(empTmp);


        Employee admin = shop.getAdmin();
        //参数空校验
        if(StringUtils.isEmpty(shop.getName())
                ||StringUtils.isEmpty(shop.getAddress())
                ||StringUtils.isEmpty(admin.getUsername())
        ){
            throw new BusinessException("参数不能为空!!!");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("不准乱写，小心打你!");
       }
        //判断店铺是否存在
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if(shopTmp!=null){
            throw new BusinessException("店铺已经存在!!忘记密码请点击找回密码");
        }

        //判断人是否已经注册
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        //注册了并且已经绑定了店铺
        if(empTmp!=null && empTmp.getShop_id()!=null){
            throw new BusinessException("用户已经有运营店铺，请重新换人");
        }
        //注册了还没有绑定店铺
        if(empTmp==null){
            //直接绑定,要返回自增主键

             //我们在保存员工的时候，需要保存logininfo，不建议在这里写，应该将业务交给service自己玩
             //-- 业务的高度封装
             //shopMapper.save(shop);
            employeeService.save(admin);
            empTmp = admin;
        }
        // 保存数据
        // 再反过来修改shop中的admin_id
        shopMapper.save(shop);
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);
        shop.setAdmin_id(empTmp.getId());
        shopMapper.update(shop);
    }


}
